<?php

namespace app\service;

use app\common\BaseService;
use app\model\House as HouseModel;

class HouseService extends BaseService {
  public static function getInstance(): HouseService {
    return parent::getInstance();
  }

  public static function getModel(): HouseModel {
    return HouseModel::getInstance();
  }

  /*
   * 列表
   */
  public static function getHouseList($condition, $page, $pageSize = 30, $order= ['id' => 'desc']) {
    return self::getModel()->getDataDetail($condition, $page, $pageSize, $order);
  }
  /*
   * 分页
   */
  public static function getHousePager($condition, $page, $pageSize = 30) {
    return self::getModel()->pager($condition, $page, $pageSize);
  }

  /*
   * 修改
   */
  public static function updateHouse($id, $data) {
    $data = [
      'price' => $data['price']
    ];
    $res = self::getModel()
      ->where('id', $id)
      ->update($data);
    if (!$res) {
      return ['status' => false, 'msg' => '修改失败'];
    }
    return ['status' => true, 'msg' => 'ok'];
  }

  /*
   * 获取所有数据
   */
  public static function getAllHouse($conditions, $order) {
    $result = self::getModel();
    foreach ($conditions as $item) {
      list($field, $value, $op) = $item;
      if (!$op) {
        $op = '=';
      }
      $result = $result->where($field, $op, $value);
    }
    if ($order) {
      $result = $result->order($order);
    }
    return $result->select()->toArray();
  }
}